package love.yuanmeng.guetcoalsystem.service.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/15 17:02
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import love.yuanmeng.guetcoalsystem.mapper.BondMapper;
import love.yuanmeng.guetcoalsystem.mapper.OrderMapper;
import love.yuanmeng.guetcoalsystem.mapper.PropMapper;
import love.yuanmeng.guetcoalsystem.pojo.Bond;
import love.yuanmeng.guetcoalsystem.pojo.Order;
import love.yuanmeng.guetcoalsystem.pojo.Prop;
import love.yuanmeng.guetcoalsystem.service.OrderService;
import love.yuanmeng.guetcoalsystem.util.RandomUtils;
import love.yuanmeng.guetcoalsystem.vo.OrderPropNameVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;

    @Resource
    PropMapper propMapper;

    @Resource
    BondMapper bondMapper;

    @Override
    public List<OrderPropNameVo> loadAllOrder(String en_id) {
        return orderMapper.loadAllOrder(en_id);
    }

    @Override
    public String updateOrder(String order_id, String id) {
        String finish_date=RandomUtils.getTime();
        Prop prop=null;
        Order order=null;
        Bond bond1=null;
        Bond bond2=null;
        prop=propMapper.selectprop(id);
        order=orderMapper.selectById(order_id);
        if(orderMapper.updateOrder("3",finish_date,order_id)==1&propMapper.updateProp("3",id)==1){
            if(prop!=null&order!=null){
                float price = prop.getSupplyQuantity() * prop.getBondNumber()*10000;
                String enter1=order.getEnterpriseId1();
                String enter2=order.getEnterpriseId2();
                bond1=bondMapper.selectbo(enter1);//查找卖方
                bond2=bondMapper.selectbo(enter2);//查找买方
                String unfrozen_amount1=String.valueOf(Float.parseFloat(bond1.getUnfrozen_amount())+price);
                String contract_frozen_amount1=String.valueOf(Float.parseFloat(bond1.getContract_frozen_amount())-price);
                int a=bondMapper.updatebo2(String.valueOf(bond1.getBond_id()),unfrozen_amount1,contract_frozen_amount1);

                String unfrozen_amount2=String.valueOf(Float.parseFloat(bond2.getUnfrozen_amount())+price);
                String contract_frozen_amount2=String.valueOf(Float.parseFloat(bond2.getContract_frozen_amount())-price);
                int b=bondMapper.updatebo2(String.valueOf(bond2.getBond_id()),unfrozen_amount2,contract_frozen_amount2);
                if(a==1&b==1)
                    return "SUCCESS";
                else
                    return "ERROR";
            }
            return "ERROR";
        }
        else
            return "ERROR";
    }

}
